/**
 * Created by Administrator on 2017/1/16.
 */

var MJPlayer = require('../MJ/player').MJPlayer;
var MJ = require('../MJ/MJ');
var Const = require('./const');
var SubCode = require('../subCode');

exports.TCMJPlayer = MJPlayer.extend({
    _selectedHSZCards: [],

    init: function(uid, msg) {
        this._super(uid, msg);

        this._stat.zmCount = 0;             // 自摸次数
        this._stat.dpCount = 0;             // 点炮次数
        this._stat.dhCount = 0;             // 点胡次数
        this._stat.anGangCount = 0;        // 暗杠次数
        this._stat.mingGangCount = 0;      // 明杠次数

        this._selectedHSZCards = [];
    },

    getSelectedHSZCards: function() {
        return this._selectedHSZCards;
    },

    selectHSZCards: function(cards) {
        if (this._selectedHSZCards.length > 0) {
            return SubCode.TCMJ_HSZ_CARDS_SELECTED;
        }

        var colors = {};
        var objCards = {};
        cards.forEach(function(card) {
            ExObject.addNumber(objCards, card, 1);
            ExObject.addNumber(colors, MJ.getColor(card), 1);
        });

        if (Object.keys(colors).length != 1) {
            return SubCode.TCMJ_HSZ_CARDS_ERROR;
        }

        var cardEnough = true;
        ExObject.eachKeyNum(objCards, function(card, num) {
            if (!card || this._handCards.getCardNum(card) < num) {
                cardEnough = false;
            }
        }.bind(this));

        if (!cardEnough) {
            return SubCode.TCMJ_HSZ_CARDS_ERROR;
        }

        this._selectedHSZCards = cards;
        return Code.SUCCESS;
    },

    exchangeHSZCards: function(cards) {
        var handCards = this._handCards;

        this._selectedHSZCards.forEach(function(card) {
            handCards.delCard(card);
        });

        handCards.init(cards);

        this.sendReplay("onHSZCardExchanged", {
            playerIndex: this.getIndex(),
            cards: cards
        });
    },

    settlementGangScore: function() {
        var player = this;
        var room = this.getRoom();

        if (!this.isRoundTing()) {
            return;
        }

        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.overdue || gangInfo.unscored) {
                continue;
            }

            var score = Const.GangScore[gangInfo.gang] || 0;
            gangInfo.targets.forEach(function(targetIndex) {
                var targetPlayer = room.getPlayerByIndex(targetIndex);
                if (targetPlayer) {
                    player.addRoundGangScore(score);
                    targetPlayer.addRoundGangScore(-score);
                }
            });
        }
    },

    settlementPatternScore: function() {
        if (!this.isRoundHu()) {
            return;
        }

        var player = this;
        var room = this.getRoom();

        // 结算牌型及基础分
        var itemScore = ExObject.sumValue(this._stat.roundItemScores);
        var totalTingScore = 0;

        this._stat.roundTarget.forEach(function(playerIndex) {
            var target = room.getPlayerByIndex(playerIndex);
            if (target) {
                var patternScore = player._stat.roundPatternScore;
                if (player.getRoundPattern() == Const.Pattern.NORMAL) {
                    if (target.isBaoTing()) { // 平胡报听玩家，报听玩家不付平胡分
                        patternScore = 0;
                    }
                }

                var score = patternScore + itemScore;
                target.addRoundScore(-score);
                target.addRoundScoreDetail(player.getRoundResult(), -score);
                player.addRoundScore(score);

                if (target.isBaoTing()) {
                    var tingScore = Const.ItemScore[Const.ScoreRule.BT];
                    target.addRoundScore(-tingScore);
                    target.addRoundScoreDetail(Const.ScoreReason.BT, -tingScore);
                    totalTingScore += tingScore;
                }
            }
        });

        if (totalTingScore > 0) {
            player.addRoundScore(totalTingScore);
            player.addRoundScoreDetail(Const.ScoreReason.BT, totalTingScore);
        }
    },

    settlementTingScore: function() {
        var player = this;
        var room = this.getRoom();

        if (player.isRoundTing()) {
            return;
        }

        var totalScore = 0;
        room.enumPlayers(function(eIndex, ePlayer) {
            if ((player.getIndex() != eIndex) && ePlayer.isRoundTing() && !ePlayer.isRoundHu()) {
                var score = Const.PatternScore[ePlayer.getRoundPattern()] || 0;
                totalScore += score;
                ePlayer.addRoundScore(score);
                ePlayer.addRoundScoreDetail(Const.ScoreReason.PT, score);

                if (ePlayer.isBaoTing()) {
                    var tingScore = Const.ItemScore[Const.ScoreRule.BT];
                    ePlayer.addRoundScore(tingScore);
                    ePlayer.addRoundScoreDetail(Const.ScoreReason.BT, tingScore);
                    totalScore += tingScore;
                }
            }
        });

        player.addRoundScore(-totalScore);
        player.addRoundScoreDetail(Const.ScoreReason.PT, -totalScore);
    },

    settlement: function() {
        this._super();

        this.addRoundScore(this._stat.roundGangScore);

        if (this._stat.roundResult == MJ.Result.ZM) {
            this._stat.zmCount++;
        } else if (this._stat.roundResult == MJ.Result.DH) {
            this._stat.dhCount++;
            if (this._stat.roundTarget.length > 0) {
                var dpPlayer = this.getRoom().getPlayerByIndex(this._stat.roundTarget[0]);
                if (dpPlayer) {
                    dpPlayer._stat.dpCount++;
                }
            }
        }

        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                this._stat.anGangCount++;
            } else {
                this._stat.mingGangCount++;
            }
        }
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getReconnectInfo: function(isSelf) {
        var info = this._super(isSelf);
        info.selectedHSZCards = this._selectedHSZCards;
        if (!isSelf) {
            info.selectedHSZCards = clone(this._selectedHSZCards);
            for (var i = 0, l = info.selectedHSZCards.length; i < l; ++i) {
                info.selectedHSZCards[i] = 1;
            }
        }

        return info;
    },

    getSettlementInfo: function() {
        var info = this._super();
        info.gangCards = this._gangCards;
        info.pengCards = this._pengCards;
        info.handCards = this._handCards.getCards();
        return info;
    },

    onStartNewRound: function() {
        this._super();
        this._selectedHSZCards = [];
    },

    getInitHandCardPreCards: function() {
        var preCards = [];

        //var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
        //if (distance == 0) {
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 5));
        //    preCards.push(MJ.getValue(MJ.Color.TIAO, 5));
        //} else if (distance == 1) {
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 1));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 2));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 3));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 4));
        //    preCards.push(MJ.getValue(MJ.Color.TONG, 5));
        //} else {
        //
        //}

        return preCards;
    },

    cancelLastGangForQG: function(card) {
        var gangInfo = this._super(card);
        if (gangInfo) {
            var gangNum = this.getRoom().getCurGang();
            if (gangNum > 1 && this._gangCards.length > 0) {
                this._gangCards[this._gangCards.length - 1].unscored = 1;
            }
        }
    },

    checkHuLogic: function(cards, huCard) {
        var pattern = this._super(cards, huCard);
        if (pattern == MJ.Pattern.NONE) {
            return pattern;
        }

        var colors = this.getColorsWithCards(cards);

        var exPattern = 0;
        if (colors.length == 1) { // 清一色
            exPattern = 5;
        }

        if (huCard == 0) { // 天胡
            if (this._handCards.getAnGangCards().length > 0) {
                exPattern += 2;
            }
        } else {
            var pengIndex = this._pengCards.indexOf(huCard);
            if (pengIndex != -1) {
                if (pattern == MJ.Pattern.NORMAL) {
                    exPattern += 1;
                }
            } else if (cards[huCard] == 4) {
                if (pattern == MJ.Pattern.NORMAL || pattern == MJ.Pattern.PAIR7) {
                    exPattern += 2;
                }
            }
        }

        if (exPattern) {
            pattern = pattern*10 + exPattern;
        }

        return pattern;
    },
});